<?php
/**
 * Created by PhpStorm.
 * User: function
 * Date: 2018-04-13
 * Time: 0:26
 */

namespace app\api\controller\v1;
use app\common\controller\Api;
use app\common\logic\FavoriteLogic;

class Favorite extends Api
{
    public function index()
    {
        $params = paramForm();
        $logic = new FavoriteLogic();
        
        
        if(isset($params['action'])){
            switch ($params['action']){
                case 'add':
                    $this->save($params);
                    break;
                case 'del':
                    $this->del($params);
                    break;
                default:
                    break;
            }
        }
        
        $params['user_id'] = $this->getUserId();//补齐用户的参数
//        var_dump($params);die;
        $lists = $logic->getList($params);
        if($lists){
           
            success($lists);
        }else{
            error('没有符合的数据');
        }
        
    }
    
    public function save($params){
        
        $rule=[
            'type|类型'=>'require|between:1,3',
            'resource_id|资源id'=>'require',
            'user_id|用户id'=>'require'
        ];
        $params['user_id'] = $this->getUserId();
        $this->validate($params,$rule);
        
        
        $logic = new FavoriteLogic();
       
        $rt = $logic->addFavorite($params['user_id'],$params['type'],$params['resource_id']);
    
        if($rt){
            success($rt);
        }else{
            error('收藏失败');
        }
        
    }
    
    public function del($params){
        $rule=[
            'type|类型'=>'require|between:1,3',
            'resource_id|资源id'=>'require',
            'user_id|用户id'=>'require'
        ];
        $params['user_id'] = $this->getUserId();
        $this->validate($params,$rule);
    
    
        $logic = new FavoriteLogic();
    
        $rt = $logic->delFavorite($params['user_id'],$params['type'],$params['resource_id']);
    
        if($rt){
            success($rt);
        }else{
            error('取消收藏失败');
        }
    }
    
}